package cn.com.allunion.sms.service.mybatis.impl;

import cn.com.allunion.common.DateModule;
import cn.com.allunion.common.UUIDGenerator;
import cn.com.allunion.common.page.QueryResult;
import cn.com.allunion.sms.mapper.SmsbusinessMapper;
import cn.com.allunion.sms.mapper.UserbaseMapper;
import cn.com.allunion.sms.mapper.UserbaseSmsbusinessMapper;
import cn.com.allunion.sms.pojo.Smsbusiness;
import cn.com.allunion.sms.pojo.SmsbusinessExample;
import cn.com.allunion.sms.pojo.UserbaseSmsbusiness;
import cn.com.allunion.sms.pojo.UserbaseSmsbusinessExample;
import cn.com.allunion.sms.service.mybatis.SmsbusinessService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 平台业务业务逻辑接口 实现类
 * Created by YangJie on 2016/4/15.
 */
@Service
@Transactional
public class SmsbusinessServiceImpl implements SmsbusinessService {

    @Autowired
    SmsbusinessMapper smsbusinessMapper ;

    @Autowired
    UserbaseMapper userbaseMapper ;

    @Autowired
    UserbaseSmsbusinessMapper userbaseSmsbusinessMapper ;

    @Override
    public String addNewBusiness(String smsBusinessName, String smsBusinessDesc, String businessCode, String businessState, String businessJoinNum, String businessSignature) {
        Smsbusiness smsBusiness = new Smsbusiness();
        smsBusiness.setSmsBusinessID(UUIDGenerator.generatorRandomUUID());
        smsBusiness.setSmsBusinessName(smsBusinessName);
        smsBusiness.setSmsBusinessDesc(smsBusinessDesc);
        smsBusiness.setBusinessCode(businessCode);
        smsBusiness.setBusinessState(businessState);
        smsBusiness.setBusinessJoinNum(businessJoinNum);
        smsBusiness.setBusinessSignature(businessSignature);

        int result = smsbusinessMapper.insert(smsBusiness) ;
        if (result > 0) {
            return "success";
        }

        return "failure" ;
    }

    @Override
    public void saveOrUpdate(Smsbusiness sms) {
        smsbusinessMapper.insert(sms) ;
    }

    @Override
    public String updateBusiness(String id, String smsBusinessName, String smsBusinessDesc, String businessCode, String businessState, String businessJoinNum, String businessSignature) {
        Smsbusiness smsb = smsbusinessMapper.selectByPrimaryKey(id);
        smsb.setSmsBusinessName(smsBusinessName);
        smsb.setSmsBusinessDesc(smsBusinessDesc);
        smsb.setBusinessCode(businessCode);
        smsb.setBusinessState(businessState);
        smsb.setBusinessJoinNum(businessJoinNum);
        smsb.setBusinessSignature(businessSignature);
        smsb.setUpdateDate(new Date());
        int result = smsbusinessMapper.updateByPrimaryKey(smsb) ;
        if (result > 0) {
            return "success";
        }

        return "failure" ;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public Smsbusiness findbyId(String id) {
        return smsbusinessMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public List<Smsbusiness> findAllSmsBusiness() {
        SmsbusinessExample example = new SmsbusinessExample() ;
        return smsbusinessMapper.selectByExample(example) ;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public QueryResult<Smsbusiness> findAllSmsbusiness(int pageCurrent, int pageSize) {
        SmsbusinessExample example = new SmsbusinessExample() ;
        PageHelper.startPage(pageCurrent, pageSize) ; // 开始分页，默认会查询总记录条数
        List<Smsbusiness> smsbusinessList = smsbusinessMapper.selectByExample(example);

        PageInfo<Smsbusiness> pageInfo = new PageInfo<>(smsbusinessList) ;
        QueryResult<Smsbusiness> queryResult = new QueryResult<>(smsbusinessList, pageInfo.getTotal()) ;

        return queryResult;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public List<Smsbusiness> findAllSmsbusiness(String userId) {
        // 得到用户所拥有的业务列表
        List<Smsbusiness> smsbUser = findSmsbusinessbyUserId(userId);
        List<Smsbusiness> allSmsBusiness = findAllSmsBusiness();
        allSmsBusiness.remove(smsbUser) ;

        return allSmsBusiness ;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public List<Smsbusiness> findSmsbusinessbyUserId(String userId) {
//        Userbase userbase = userbaseMapper.selectByPrimaryKey(userId) ;
        UserbaseSmsbusinessExample example = new UserbaseSmsbusinessExample() ;
        UserbaseSmsbusinessExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId) ;
        List<UserbaseSmsbusiness> usbList = userbaseSmsbusinessMapper.selectByExample(example);
        if (null != usbList && usbList.size()>0) {
            List<Smsbusiness> smsbs = new ArrayList<>(usbList.size()) ;
            for (UserbaseSmsbusiness usb : usbList) {
                smsbs.add(smsbusinessMapper.selectByPrimaryKey(usb.getSmsBusinessId()));
            }
            return smsbs ;
        }

        return null ;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public int findState(String id) {
        Smsbusiness smsb = smsbusinessMapper.selectByPrimaryKey(id) ;
        if (null != smsb) {
            return Integer.parseInt(smsb.getBusinessState());
        }
        return -2;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public List<Smsbusiness> findbyCode(String code) {
        SmsbusinessExample example = new SmsbusinessExample() ;
        SmsbusinessExample.Criteria criteria = example.createCriteria() ;
        criteria.andBusinessCodeEqualTo(code) ;
        return smsbusinessMapper.selectByExample(example) ;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public Smsbusiness findBussinessObj(String businessCode) {
        List<Smsbusiness> smsbList = findbyCode(businessCode);
        if (null != smsbList && smsbList.size() != 0) {
            return smsbList.get(0);
        } else {
            return null;
        }
    }
}
